<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $row = (int)$_POST["row"];
    $col = (int)$_POST["col"];
    $player = (int)$_POST["player"];
    $opponent = $player === 1 ? 2 : 1;

    $board = $_SESSION['board'];

    // 检查落子是否合法
    if ($board[$row][$col] !== 0 ||!isValidMove($board, $row, $col, $player)) {
        $response = [
            'success' => false,
            'board' => $board,
            'winner' => null,
            'draw' => false,
            'nextPlayer' => $player
        ];
    } else {
        // 落子并翻转棋子
        $board[$row][$col] = $player;
        flipPieces($board, $row, $col, $player);

        // 检查下一个玩家是否有合法落子
        $nextPlayerHasMove = false;
        for ($i = 0; $i < 8; $i++) {
            for ($j = 0; $j < 8; $j++) {
                if ($board[$i][$j] === 0 && isValidMove($board, $i, $j, $opponent)) {
                    $nextPlayerHasMove = true;
                    break 2;
                }
            }
        }
        $nextPlayer = $nextPlayerHasMove ? $opponent : $player;

        // 检查游戏是否结束
        $winner = null;
        $draw = false;
        $totalBlack = 0;
        $totalWhite = 0;
        $noMovesLeft = true;
        for ($i = 0; $i < 8; $i++) {
            for ($j = 0; $j < 8; $j++) {
                if ($board[$i][$j] === 0) {
                    if (isValidMove($board, $i, $j, 1) || isValidMove($board, $i, $j, 2)) {
                        $noMovesLeft = false;
                    }
                }
                if ($board[$i][$j] === 1) {
                    $totalBlack++;
                } elseif ($board[$i][$j] === 2) {
                    $totalWhite++;
                }
            }
        }
        if ($noMovesLeft) {
            if ($totalBlack > $totalWhite) {
                $winner = 1;
            } elseif ($totalWhite > $totalBlack) {
                $winner = 2;
            } else {
                $draw = true;
            }
        }

        $_SESSION['board'] = $board;
        $_SESSION['currentPlayer'] = $nextPlayer;

        $response = [
            'success' => true,
            'board' => $board,
            'winner' => $winner,
            'draw' => $draw,
            'nextPlayer' => $nextPlayer
        ];
    }

    header('Content-Type: application/json');
    echo json_encode($response);
}

function isValidMove($board, $row, $col, $player) {
    $opponent = $player === 1 ? 2 : 1;
    $directions = [
        [-1, -1], [-1, 0], [-1, 1],
        [0, -1], [0, 1],
        [1, -1], [1, 0], [1, 1]
    ];

    foreach ($directions as $dir) {
        $r = $row + $dir[0];
        $c = $col + $dir[1];
        $foundOpponent = false;
        while ($r >= 0 && $r < 8 && $c >= 0 && $c < 8) {
            if ($board[$r][$c] === $opponent) {
                $foundOpponent = true;
            } elseif ($board[$r][$c] === $player) {
                if ($foundOpponent) {
                    return true;
                }
                break;
            } else {
                break;
            }
            $r += $dir[0];
            $c += $dir[1];
        }
    }
    return false;
}

function flipPieces($board, $row, $col, $player) {
    $opponent = $player === 1 ? 2 : 1;
    $directions = [
        [-1, -1], [-1, 0], [-1, 1],
        [0, -1], [0, 1],
        [1, -1], [1, 0], [1, 1]
    ];

    foreach ($directions as $dir) {
        $r = $row + $dir[0];
        $c = $col + $dir[1];
        $piecesToFlip = [];
        while ($r >= 0 && $r < 8 && $c >= 0 && $c < 8) {
            if ($board[$r][$c] === $opponent) {
                $piecesToFlip[] = [$r, $c];
            } elseif ($board[$r][$c] === $player) {
                foreach ($piecesToFlip as $piece) {
                    $board[$piece[0]][$piece[1]] = $player;
                }
                break;
            } else {
                break;
            }
            $r += $dir[0];
            $c += $dir[1];
        }
    }
    return $board;
}
?>